# 通过python-jira模块查询jira的问题
from jira import JIRA


class StaticJira:
    def __init__(self, jira_server, jira_user, jira_password):
        self.jira = JIRA(server=jira_server, basic_auth=(jira_user, jira_password))
        self.jira_search = self.jira.search_issues

    def search(self, jql):
        # 错误处理
        try:
            return self.jira_search(jql, maxResults=1000)
        except Exception as e:
            print(e)
            return None

    def get_issue_by_version(
        self,
        version=None,
        project=None,
        status=None,
        resolution=None,
        start=None,
        limit=10,
        order_by=None,
        asc=True,
        fields=None,
    ):
        # 获取指定版本的问题
        jql = "fixVersion = {1}".format(project, version)
        if status:
            jql += " AND status = {0}".format(status)
        if resolution:
            jql += " AND resolution = {0}".format(resolution)
        if start:
            jql += " AND created >= {0}".format(start)
        if project:
            jql = " AND project = {0} ".format(project)
        if order_by:
            jql += " ORDER BY {0}".format(order_by)
            if asc:
                jql += " ASC"
            else:
                jql += " DESC"
        if fields:
            jql += " {0}".format(fields)
        print(jql)
        return self.search(jql)

    def obtain_reopening_issues(self, version, project="ITWRZL"):
        # 获取指定版本重开的问题
        jql = "project = {0} AND status = Reopened AND fixVersion = {1}".format(
            project, version
        )
        return self.search(jql)

    def get_issue(self, issue_id):
        return self.jira.issue(issue_id)

    def get_issue_comments(self, issue_id):
        return self.jira.comments(issue_id)

    def get_issue_worklogs(self, issue_id):
        return self.jira.worklogs(issue_id)

    def get_issue_transitions(self, issue_id):
        return self.jira.transitions(issue_id)

    def get_issue_transition_by_id(self, issue_id, transition_id):
        return self.jira.transition_issue(issue_id, transition_id)

    def get_issue_transition_by_name(self, issue_id, transition_name):
        return self.jira.transition_issue(issue_id, transition_name)

    def get_issue_transition_by_field(self, issue_id, transition_field):
        return self.jira.transition_issue(issue_id, transition_field)

    def get_issue_transition_by_comment(self, issue_id, transition_comment):
        return self.jira.transition_issue(issue_id, transition_comment)

    def get_issue_transition_by_input(self, issue_id, transition_input):
        return self.jira.transition_issue(issue_id, transition_input)

    def get_issue_transition_by_params(self, issue_id, transition_params):
        return self.jira.transition_issue(issue_id, transition_params)

    def get_issue_transition_by_transition(self, issue_id, transition_transition):
        return self.jira.transition_issue(issue_id, transition_transition)

    def get_issue_transition_by_update(self, issue_id, transition_update):
        return self.jira.transition_issue(issue_id, transition_update)

    def get_issue_transition_by_fields(self, issue_id, transition_fields):
        return self.jira.transition_issue(issue_id, transition_fields)

    def get_issue_transition_by_project(self, issue_id, transition_project):
        return self.jira.transition_issue(issue_id, transition_project)

    def get_issue_list_by_project(self, project):
        return self.jira.search_issues("project={0}".format(project))


if __name__ == "__main__":
    p = StaticJira("http://172.16.2.2:57242", "zhangyilin", "9854353065")
    issues = p.get_issue_by_version("2.1.3P14")
    for i in issues:
        print(i.raw)